package com.freemalll.system.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.freemalll.common.core.utils.StringUtils;
import com.freemalll.common.core.utils.poi.ExcelUtil;
import com.freemalll.common.core.web.controller.BaseController;
import com.freemalll.common.core.web.domain.AjaxResult;
import com.freemalll.common.core.web.page.TableDataInfo;
import com.freemalll.common.log.annotation.Log;
import com.freemalll.common.log.enums.BusinessType;
import com.freemalll.common.security.annotation.RequiresPermissions;
import com.freemalll.common.security.utils.SecurityUtils;
import com.freemalll.system.domain.SysRole;
import com.freemalll.system.service.ISysRoleService;

/**
 * 角色 信息操作处理
 * 
 * @author freemalll
 */
@RestController
@RequestMapping("/role")
public class SysRoleController extends BaseController
{
    @Autowired
    private ISysRoleService roleService;

    /**
     * 获取角色列表
     */
    @RequiresPermissions("system:role:list")
    @GetMapping("/list")
    public TableDataInfo list(SysRole role)
    {
        startPage();
        List<SysRole> list = roleService.selectRoleList(role);
        return getDataTable(list);
    }

    @Log(title = "角色管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:role:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRole role)
    {
        List<SysRole> list = roleService.selectRoleList(role);
        ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class);
        util.exportExcel(response, list, "角色数据");
    }

    /**
     * 根据角色编号获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id)
    {
        return success(roleService.selectRoleById(id));
    }

    /**
     * 根据角色编码查询角色信息
     */
    @GetMapping(value = "/roleCode/{roleCode}")
    public AjaxResult getInfoByRoleCode(@PathVariable String roleCode)
    {
        return success(roleService.selectRoleByRoleCode(roleCode));
    }

    /**
     * 根据角色名称查询角色信息
     */
    @GetMapping(value = "/roleName/{roleName}")
    public AjaxResult getInfoByRoleName(@PathVariable String roleName)
    {
        return success(roleService.selectRoleByRoleName(roleName));
    }

    /**
     * 新增角色
     */
    @RequiresPermissions("system:role:add")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysRole role)
    {
        if (!roleService.checkRoleCodeUnique(role))
        {
            return error("新增角色'" + role.getRoleName() + "'失败，角色编码已存在");
        }
        if (StringUtils.isNotEmpty(role.getRoleName()) && !roleService.checkRoleNameUnique(role))
        {
            return error("新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
        }
        role.setCreateName(SecurityUtils.getUsername());
        return toAjax(roleService.insertRole(role));
    }

    /**
     * 修改角色
     */
    @RequiresPermissions("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysRole role)
    {
        if (!roleService.checkRoleCodeUnique(role))
        {
            return error("修改角色'" + role.getRoleName() + "'失败，角色编码已存在");
        }
        if (StringUtils.isNotEmpty(role.getRoleName()) && !roleService.checkRoleNameUnique(role))
        {
            return error("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
        }
        role.setUpdateName(SecurityUtils.getUsername());
        return toAjax(roleService.updateRole(role));
    }

    /**
     * 删除角色
     */
    @RequiresPermissions("system:role:remove")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        roleService.deleteRoleByIds(ids);
        return success();
    }

    /**
     * 修改角色状态
     */
    @RequiresPermissions("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysRole role)
    {
        role.setUpdateName(SecurityUtils.getUsername());
        return toAjax(roleService.changeRoleStatus(role));
    }
}